package com.sense.kukoo.biz_me.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.event.LoginSuccessEvent
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.http.bean.LoginResultBean
import com.sense.kukoo.common.repo.http.bean.UserInfoBean
import com.sense.kukoo.common.repo.kv.login_history.LoginHistory
import org.greenrobot.eventbus.EventBus

class VerifyCodeViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    val mIsShowPhoneErrorHit: MutableLiveData<Boolean> = MutableLiveData(false)
    val mIsShowSentTime:MutableLiveData<Boolean> = MutableLiveData(true)
    val mIsShowRecent:MutableLiveData<Boolean> = MutableLiveData(false)
    val mBtnEnable: MutableLiveData<Boolean> = MutableLiveData(false)

    val action_toCountDownTimer: MutableLiveData<Boolean> = MutableLiveData()
    val action_toLoginSuccess: MutableLiveData<Boolean> = MutableLiveData()

    private var mCurrentVerifyCode:String = ""
    private lateinit var from_area:String
    private lateinit var from_phone:String

    override fun createModel(): BaseMvvmModel? = null

    fun setFromData(areaCode: String, mobile: String){
        from_area=areaCode
        from_phone=mobile
    }

    override fun initData() {
        onRecentClick()
    }

    fun onRecentClick(){
        toView(ACTION_SHOW_DIALOG)
        request {
            //
            val result = Repo.http.sendVerifyCode( "+" + from_area, from_phone)
            val data:Boolean = result.data == true
            if (result.success && data) {
                mIsShowSentTime.postValue(true)
                mIsShowRecent.postValue(false)
                action_toCountDownTimer.postValue(true)
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
                mIsShowRecent.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun onCountDownTimerFinish(){
        mIsShowSentTime.postValue(false)
        mIsShowRecent.postValue(true)
    }

    fun onVerifyCodeChanged(code:String?){
        mCurrentVerifyCode = code ?: ""
        if(mCurrentVerifyCode.length != 4){
            mBtnEnable.postValue(false)
        }else{
            mBtnEnable.postValue(true)
        }
        mIsShowPhoneErrorHit.postValue(false)
    }

    fun onEnterClick(){
        if(mCurrentVerifyCode.length != 4){
            mIsShowPhoneErrorHit.postValue(true)
        }else{
            toView(ACTION_SHOW_DIALOG)
            request {
                //
                val result = Repo.http.login(from_phone, from_area, null, mCurrentVerifyCode)
                if (result.code == ResultBean.CODE_SUCCESS && result.success) {
                    val data: LoginResultBean = result.data!!
                    Repo.loginHistory.setLoginType(LoginHistory.SOURCE_INNER)
                    Repo.login.setLogin(data)
                    //
                    val userInfo = Repo.http.getUserInfo(0L)
                    val user: UserInfoBean = userInfo.data!!
                    Repo.user.setUser(user)
                    //
                    EventBus.getDefault().post(LoginSuccessEvent())
                    //
                    action_toLoginSuccess.postValue(true)
                } else {
                    Log.e(TAG, "result=" + result.msg)
                    mIsShowPhoneErrorHit.postValue(true)
                    result.msg.let { sendHintMessage(it) }
                }
                toView(ACTION_HIDE_DIALOG)
            }
        }
    }
}